package com.pet.platform.controller.admin;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.ServiceDTO;
import com.pet.platform.dto.ServiceQueryDTO;
import com.pet.platform.entity.Service;
import com.pet.platform.service.ServiceService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 管理员服务控制器
 */
@RestController
@RequestMapping("/api/admin/services")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminServiceController {
    
    private final ServiceService serviceService;
    
    /**
     * 获取服务列表
     */
    @GetMapping
    public Result<PageResult<ServiceDTO>> getServiceList(ServiceQueryDTO queryDTO) {
        return Result.success(serviceService.findPage(queryDTO));
    }
    
    /**
     * 获取服务详情
     */
    @GetMapping("/{id}")
    public Result<ServiceDTO> getServiceDetail(@PathVariable Long id) {
        Service service = serviceService.findById(id);
        ServiceDTO serviceDTO = convertToDTO(service);
        return Result.success(serviceDTO);
    }
    
    /**
     * 将Service实体转换为ServiceDTO
     */
    private ServiceDTO convertToDTO(Service service) {
        if (service == null) {
            return null;
        }
        
        ServiceDTO dto = new ServiceDTO();
        dto.setId(service.getId());
        dto.setName(service.getName());
        dto.setCategory(service.getCategory());
        dto.setDescription(service.getDescription());
        dto.setPrice(service.getPrice());
        dto.setDuration(service.getDuration());
        dto.setStatus(service.getStatus());
        
        return dto;
    }
    
    /**
     * 创建服务
     */
    @PostMapping
    public Result<Void> createService(@Valid @RequestBody ServiceDTO serviceDTO) {
        serviceService.create(serviceDTO);
        return Result.success();
    }
    
    /**
     * 更新服务
     */
    @PutMapping("/{id}")
    public Result<Void> updateService(@PathVariable Long id, @Valid @RequestBody ServiceDTO serviceDTO) {
        serviceService.update(id, serviceDTO);
        return Result.success();
    }
    
    /**
     * 删除服务
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteService(@PathVariable Long id) {
        serviceService.delete(id);
        return Result.success();
    }
    
    /**
     * 更新服务状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updateServiceStatus(@PathVariable Long id, @RequestParam Integer status) {
        serviceService.updateStatus(id, status);
        return Result.success();
    }
} 